package managers.dateManager.beginFrozen.resourceChange;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;

import org.apache.log4j.Logger;
import org.tbjd.client.beans.Resource;
import org.tbjd.client.beans.Task;
import org.tbjd.client.constants.NotificationsConstants;
import org.tbjd.client.exceptions.DateManagerException;
import org.tbjd.client.managers.TaskManager;

import factories.ResourceFactory;
import factories.TaskFactory;

/**
 * <p>
 * Test TaskManager's methods
 * </p>
 * <p>
 * <b>Case</b>
 * </p>
 * <p>
 * A task with 2 predecessors and everyone in ASAP ChainingMode
 * </p>
 * <p>
 * This task has one resource at 100%
 * </p>
 * <p>
 * <b>Action</b>
 * </p>
 * <p>
 * Ratio of the resource change to 50%
 * </p>
 * Expected
 * <ul>
 * <li>update EndDate of "task"</li>
 * <li>update Duration of "task"</li>
 * </ul>
 * </p>
 * 
 * @author Michael Martin
 * @author Antonio Carrozzini
 * @version 1.0
 * 
 */
public class BeginFrozen_ResourceChange_UpdatePredecessors_Basic extends TestCase {

	/*
	 * Task		Dates before	 		Successor 	ChainingMode	Resource	Dates after with ratio 50%
	 * p1		15.6.2009 - 17.6.2009 	task		ASAP 			-			15.6.2009 - 17.6.2009
	 * p2		18.6.2009 - 21.6.2009 	task		ASAP 			-			18.6.2009 - 21.6.2009
	 * task 	22.6.2009 - 25.6.2009 	-		 	ASAP 			100%		22.6.2009 - 28.1.2009
	 */

	private Map<Integer, Object> map = null;
	private Logger log = Logger.getLogger("console");
	private DateFormat df = DateFormat.getInstance();
	private Task task = null;
	private TaskManager dateManager = new TaskManager();

	protected void setUp() throws Exception {
		df = new SimpleDateFormat("dd.MM.yyyy");

		Task p1 = TaskFactory.getInstance();
		p1.setName("p1");
		p1.setBeginDate(df.parse("15.6.2009"));
		p1.setEndDate(df.parse("17.6.2009"));
		p1.setDuration(48);

		Task p2 = TaskFactory.getInstance();
		p2.setName("p2");
		p2.setBeginDate(df.parse("18.6.2009"));
		p2.setEndDate(df.parse("21.6.2009"));
		p2.setDuration(72);

		this.task = TaskFactory.getInstance();
		this.task.setName("task");
		this.task.setBeginDate(df.parse("22.6.2009"));
		this.task.setEndDate(df.parse("25.6.2009"));
		this.task.setDuration(72);
		this.task.setEffort(72);

		p1.addSuccessor(this.task);
		p2.addSuccessor(this.task);
		
		Resource resource = ResourceFactory.getInstance();
		resource.addTask(this.task);
		

		super.setUp();
	}

	public void testReturnedListNotNull() {
		System.out.println("");
		log.debug(" === testReturnedListNotNull");

		int newRatio = -1;
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		try {
			assertNotNull("Updated bean list isn't null", dateManager.manageDates(map));

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}

	}

	public void testReturnedListSize() {
		System.out.println("");
		log.debug(" === testReturnedListSize");

		int newRatio = 50;
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		assertEquals("Updated bean list size = 1", 1, updatedBeans.size());

	}

	public void testDates() {
		System.out.println("");
		log.debug(" === testDates");

		int newRatio = 50;
		this.task.getResources().get(0).setRatio(newRatio);
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		
		if(updatedBeans.size() < 0){
			fail();
		}

		for (Task updatedBean : updatedBeans) {
			log.debug(updatedBean.getName() + " \t updated with BeginDate = " + df.format(updatedBean.getBeginDate()) + " , EndDate = "
					+ df.format(updatedBean.getEndDate()));
			try {
				if (updatedBean.getName().equalsIgnoreCase("p1")) {
					fail("p1 dates must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("p2")) {
					fail("p2 dates must not change");
				}
				if (updatedBean.getName().equalsIgnoreCase("task")) {
					assertEquals("check task BeginDate", df.parseObject("22.6.2009"), updatedBean.getBeginDate());
					assertEquals("check task EndDate", df.parseObject("28.6.2009"), updatedBean.getEndDate());
				}

			} catch (ParseException e) {
				fail(e.getMessage());

			}
		}
	}

	public void testChainingModes() {
		System.out.println("");
		log.debug(" === testChainingModes");

		int newRatio = 50;
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		
		if(updatedBeans.size() < 0){
			fail();
		}

		for (Task updatedTask : updatedBeans) {
			if (updatedTask.getName().equalsIgnoreCase("p2")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("p1")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("task")) {
				assertTrue(updatedTask.getState().isASAPChainingMode());
			}
		}
	}
	
	public void testDurations(){
		System.out.println("");
		log.debug(" === testDurations");

		int newRatio = 50;
		this.task.getResources().get(0).setRatio(newRatio);
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		
		if(updatedBeans.size() < 0){
			fail();
		}

		for (Task updatedTask : updatedBeans) {
			if (updatedTask.getName().equalsIgnoreCase("p2")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("p1")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("task")) {
				assertEquals(144f, updatedTask.getDuration());
			}
		}
	}

	public void testEfforts(){
		System.out.println("");
		log.debug(" === testEfforts");

		int newRatio = 50;
		this.task.getResources().get(0).setRatio(newRatio);
		log.debug("new Ratio = " + newRatio);

		this.map = new HashMap<Integer, Object>();
		this.map.put(NotificationsConstants.TASKMANAGER_ACTION_resource, newRatio);
		this.map.put(NotificationsConstants.TASKMANAGER_BEAN, this.task);
		this.map.put(NotificationsConstants.TASKMANAGER_SENDER, null);

		log.debug("call TaskManager...");
		List<Task> updatedBeans = null;
		try {
			updatedBeans = dateManager.manageDates(this.map);

		} catch (DateManagerException e) {
			fail(e.getMessage());

		}
		
		if(updatedBeans.size() < 0){
			fail();
		}

		for (Task updatedTask : updatedBeans) {
			if (updatedTask.getName().equalsIgnoreCase("p2")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("p1")) {
				fail();
			}
			if (updatedTask.getName().equalsIgnoreCase("task")) {
				assertEquals(72f, updatedTask.getEffort());
			}
		}
	}
}